package com.material.vtool.controller;

import com.google.common.collect.Maps;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.utils.AliyunOssClientUtil;
import com.material.vtool.utils.DateUtil;
import com.material.vtool.utils.SysConstant;
import com.material.vtool.wechat.service.WeixinServiceDL;
import com.material.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
public class FileUploadController {
    /**
     * 上传用户头像
     * @param userId 用户ID
     * @param avatarImgFile 文件
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/uploadUserAvatarImg", method = RequestMethod.POST)
    public RespData uploadUserAvatarImg(@RequestHeader("userId") String userId, @RequestParam("file") MultipartFile avatarImgFile) throws IOException {

        String contentType = avatarImgFile.getContentType();

        String fileName = UUID.randomUUID().toString() + ".png";

        String formatDate = DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate());
        String dirName = SysConstant.MINI_PROGRAM_DIRECTORY + SysConstant.USER_AVATAR_IMG_OSS_DIR + formatDate + "/" + userId + "/";

        AliyunOssClientUtil.uploadFileByMultipartFile(avatarImgFile, dirName + fileName);

        String avatarImgUrl = SysConstant.USER_AVATAR_OSS_ACCESS_PREFIX + dirName + fileName;

        log.info("[avatarImgUrl] = " + avatarImgUrl);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);

        Map<String, String> resultMap = Maps.newHashMap();
        resultMap.put("avatarImgUrl", avatarImgUrl);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 上传用户二维码
     * @param userId 用户ID
     * @param qrCodeImgFile 文件
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/uploadUserQrCodeImg", method = RequestMethod.POST)
    public RespData uploadUserQrCodeImg(@RequestHeader("userId") String userId, @RequestParam("file") MultipartFile qrCodeImgFile) throws IOException {

        String contentType = qrCodeImgFile.getContentType();

        String fileName = UUID.randomUUID().toString() + ".png";

        String formatDate = DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate());
        String dirName = SysConstant.MINI_PROGRAM_DIRECTORY + SysConstant.USER_QRCODE_IMG_OSS_DIR + formatDate + "/" + userId + "/";

        AliyunOssClientUtil.uploadFileByMultipartFile(qrCodeImgFile, dirName + fileName);

        String qrCodeImgUrl = SysConstant.USER_QRCODE_OSS_ACCESS_PREFIX + dirName + fileName;

        log.info("[qrCodeImgUrl] = " + qrCodeImgUrl);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);

        Map<String, String> resultMap = Maps.newHashMap();
        resultMap.put("qrCodeImgUrl", qrCodeImgUrl);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 上传团队Logo
     * @param userId 用户ID
     * @param qrCodeImgFile 文件
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/uploadTeamLogoImg", method = RequestMethod.POST)
    public RespData uploadTeamLogoImg(@RequestHeader("userId") String userId, @RequestParam("file") MultipartFile qrCodeImgFile) throws IOException {

        String contentType = qrCodeImgFile.getContentType();

        String fileName = UUID.randomUUID().toString() + ".png";

        String formatDate = DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate());
        String dirName = SysConstant.MINI_PROGRAM_DIRECTORY + SysConstant.TEAM_LOGO_IMG_OSS_DIR + formatDate + "/" + userId + "/";

        AliyunOssClientUtil.uploadFileByMultipartFile(qrCodeImgFile, dirName + fileName);

        String qrCodeImgUrl = SysConstant.TEAM_LOGO_OSS_ACCESS_PREFIX + dirName + fileName;

        log.info("[qrCodeImgUrl] = " + qrCodeImgUrl);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);

        Map<String, String> resultMap = Maps.newHashMap();
        resultMap.put("qrCodeImgUrl", qrCodeImgUrl);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }
}
